package cn.wanda.bi.sunshine.platform.server.utils;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
 *  created by wangfupeng1
 */
public class JsonUtil {

    private static ThreadLocal<ObjectMapper> objMapperLocal = new ThreadLocal<ObjectMapper>() {
        @Override
        public ObjectMapper initialValue() {
            ObjectMapper mapper = new ObjectMapper();
            return mapper;
        }
    };

    public static <T> String toJson(T obj) {
        return JSON.toJSONString(obj);
    }

    public static <T> T toObject(String jsonStr, Class<T> cls) {
        return JSON.parseObject(jsonStr, cls);
    }

    public static <T> List<T> toList(String jsonString, Class<T> clazz) {
        try {
            ObjectMapper mapper = objMapperLocal.get();
            return (List<T>) mapper.readValue(jsonString, mapper.getTypeFactory()
                    .constructParametricType(List.class, clazz));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
